//
//  AWHomeTableViewCell.swift
//  WeiBo
//
//  Created by AaronWei on 2017/6/8.
//  Copyright © 2017年 AaronWei. All rights reserved.
//

import UIKit
import SnapKit

class AWHomeTableViewCell: UITableViewCell {
    
    var homeStatusesViewModel: AWStatusesViewModel? {
        
        didSet {
            
            if let picUrls = homeStatusesViewModel?.status?.retweeted_status?.pic_urls, picUrls.count > 0 {
               
            }
            
            originalView.statusesViewModel = homeStatusesViewModel
            
            toolBar.statusesViewModel = homeStatusesViewModel
            
            toolbarTopConstraints?.deactivate()
            
            if homeStatusesViewModel?.status?.retweeted_status != nil {
                
                retweetView.isHidden = false
                
                toolBar.snp.makeConstraints({ (make) in
                    
                    toolbarTopConstraints = make.top.equalTo(retweetView.snp.bottom).constraint
                    
                })
                
                retweetView.statusesViewModel = homeStatusesViewModel
                
            } else {
                
                retweetView.isHidden = true
                
                toolBar.snp.makeConstraints({ (make) in
                    
                    toolbarTopConstraints = make.top.equalTo(originalView.snp.bottom).constraint
                    
                })
                
            }
            
        }
        
    }

    var toolbarTopConstraints: Constraint?
    
    fileprivate lazy var originalView: AWHomeOriginalView = AWHomeOriginalView()
    
    fileprivate lazy var retweetView: AWHomeRetweetView = AWHomeRetweetView()
    
    fileprivate lazy var toolBar: AWHomeToolBar = AWHomeToolBar()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        setupUI()
        
    }
    
    private func setupUI() {
        
        self.selectionStyle = .none
        
        contentView.addSubview(originalView)
        
        contentView.addSubview(retweetView)
        
        contentView.addSubview(toolBar)
        
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        originalView.snp.makeConstraints { (make) in
            
            make.top.equalTo(contentView).offset(8)
            make.leading.trailing.equalTo(contentView)
            
        }
        
        retweetView.snp.makeConstraints { (make) in
            
            make.top.equalTo(originalView.snp.bottom)
            make.leading.trailing.equalTo(originalView)
            
        }
        
        toolBar.snp.makeConstraints { (make) in
            
            toolbarTopConstraints = make.top.equalTo(retweetView.snp.bottom).constraint
            make.leading.trailing.equalTo(retweetView)
            make.bottom.equalTo(contentView)
            
        }
    
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
